$(function () {
    var defaultColumns = initColumn();
    var searchParams = {};
    vm.table = new BSTable('menuTable', "/platform/menu/list", defaultColumns,searchParams);
    vm.table.setPaginationType("server");
    vm.table.init();

    // jsTree
    $('#menu-tree').on('activate_node.jstree',function (e,data) {
        vm.menu['menuUpId'] = data.node.id;
        vm.menu['menuUpName'] = data.node.text;
    });
    vm.initJsTree();

});
function initColumn () {
    return [
        {field: 'selectItem', radio: true},
        {title: '菜单ID', field: 'menuId', align: 'center', valign: 'middle',width:'50px'},
        {title: '菜单', field: 'menuName', align: 'center', valign: 'middle'},
        {title: '上级菜单', field: 'menuUpName', align: 'center', valign: 'middle'},
        {title: '链接', field: 'menuUrl', align: 'center', valign: 'middle'},
        {title: '权限标识', field: 'menuPerms', align: 'center', valign: 'middle'},
        {title: '类型', field: 'menuType', align: 'center', valign: 'middle',formatter:function (value) {
            return value == 1?"<button class='disabled btn btn-success btn-xs'>菜单</button>":"<button class='disabled btn btn-success btn-xs'>按钮</button>"
        }},
        {title: '图标', field: 'menuIcon', align: 'center', valign: 'middle',formatter:function (value) {
            return "<span class='disabled "+value+"'></span>"
        }}
    ];
};

var vm = new Vue({
    el:'#app',
    data:{
        showList:true,
        isUpdate:false,
        menu:{},
        title:'',
        table:null,
    },
    methods:{
        add:function () {
            vm.menu = {};
            vm.isUpdate = false;
            vm.showList = false;
            vm.title = '添加菜单';
        },
        chooseUpMenu:function () {
            document.getElementById("up-menu-name").blur();
            layer.open({
                type: 1,
                // offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择菜单",
                area: ['300px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#menu-tree"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    layer.close(index);
                }
            });
        },
        initJsTree:function () {
            $.ajax({
                url:'/platform/menu/adminMenu',
                dataType:'JSON',
                success:function (result) {
                    console.log(result);
                    if (result.code == 200){
                        var menus = handelMenu(result.menuTree);
                        $('#menu-tree').jstree({
                            "core":{
                                "data":menus
                            }
                        });
                    }else
                        alert(result.msg);
                },
                error:function (e) {
                    console.log("request error",e);
                    alert("请求失败");
                }
            })
        },
        update:function () {
            var data = $.getSelectData('menuTable');
            if (!data)
                return;
            vm.isUpdate = true;
            vm.showList = false;
            vm.menu = data;
            vm.title = '修改菜单信息';
        },
        del:function () {
            window.confirm('确定要删除这条数据吗？',function () {

            });
        },
        saveOrUpdate:function () {
            var urlStr = vm.isUpdate ? '/platform/menu/update':'/platform/menu/add';
            $.ajax({
                url:urlStr,
                type:'POST',
                dataType:'JSON',
                data:JSON.stringify(vm.menu),
                contentType:'application/json',
                success:function (result) {
                    if (result.code == 200){
                        alert('操作成功',function () {
                            vm.showList = true;
                            vm.table.refresh();
                        });
                    }else
                        alert(result.msg);
                },
                error:function (e) {
                    console.error("request error",e);
                    alert('请求失败');
                }
            })
        },
        cancel:function () {
            vm.showList = true;
        }
    }
});